package sw.service.impl;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.easy.excel.ExcelContext;
import org.easy.excel.result.ExcelImportResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sw.dao.AssetDao;
import sw.dao.AssetTypeDao;
import sw.dao.ProjectDao;
import sw.dao.ThreatDao;
import sw.dao.WeaknessDao;
import sw.entity.Asset;
import sw.entity.AssetType;
import sw.entity.Project;
import sw.entity.Threat;
import sw.entity.Weakness;
import sw.model.MAsset;
import sw.service.IWeaknessService;

@Service
public class WeaknessServiceImpl  implements IWeaknessService{
	@Autowired
	private ProjectDao proDao;
	
	@Autowired
	private AssetDao assetDao;
	
	@Autowired
	private AssetTypeDao assetTypeDao;
	
	@Autowired
	private ThreatDao  threadDao;
	
	@Autowired
	private WeaknessDao weaknessDao;
	
	@Override
	public Map<String, List<MAsset>> showAllWeekness(Integer projectId) {
		Project pro = proDao.findOne(projectId);
		List<Asset> assets = assetDao.findImportantAsset(projectId, pro.getImpStandard());
		if(null == assets) return null;
		Map<String, List<MAsset>> assetGroup = new HashMap<String, List<MAsset>>();
		 List<MAsset> temp;
		for(Asset asset :assets ){//针对每个资产
			AssetType type = asset.getAssetType();
			String typeName = type.getName();
			if(assetGroup.containsKey(typeName)){ 
				temp = assetGroup.get(typeName);
			}else{
				temp = new ArrayList<MAsset>();
			}
			MAsset model = new MAsset();
			List<Weakness> weakList = weaknessDao.findByAssetId(asset.getId());
			model.setMAsset(asset, weakList);//资产，脆弱性
			temp.add(model);
			assetGroup.put(typeName,temp);
		}
		return assetGroup;
	}
	
	
	/**
	 * 没有导出过，则没有数据，需要导出威胁
	 * @param projectId
	 * @return
	 * @throws Exception
	 */
	public Workbook ExportThreat(Integer projectId) throws Exception {
		List<AssetType> types = (List<AssetType>) assetTypeDao.findAll();
		//创建excel上下文实例,它的构成需要配置文件的路径
		ExcelContext context = new ExcelContext("classpath:excelConfig/WeakInit.xml");
		Project pro = proDao.findOne(projectId);
		List<Asset> assets = assetDao.findImportantAsset(projectId, pro.getImpStandard());
		if(null == assets) return null;
		Map<String, List<MAsset>> assetGroup = new HashMap<String, List<MAsset>>();
		 List<MAsset> temp;
		 List<Threat> threatLists = new ArrayList<Threat>();
		for(Asset asset :assets ){
			AssetType type = asset.getAssetType();
			String typeName = type.getName();
			if(assetGroup.containsKey(typeName)){
				temp = assetGroup.get(typeName);
			}else{
				temp = new ArrayList<MAsset>();
			}
			List<Threat> threatList = threadDao.findByAssetId(asset.getId());
			if(null != threatList){
				threatLists.addAll(threatList);
			}
		}
		Workbook workbook = new SXSSFWorkbook();
		workbook = context.createExcel(workbook,"Weakness",null,threatLists);
		return workbook;
	}
	
	@Override
	public void importExcel(String filePath, Integer projectId)
			throws Exception {
		// 创建excel上下文实例,它的构成需要配置文件的路径
		ExcelContext context = new ExcelContext("classpath:excelConfig/Weak.xml");
		InputStream excelStream = new FileInputStream(filePath);
		ExcelImportResult result;
		result = context.readExcel("Weakness", 0, excelStream, true);
		if (result.hasErrors()) {// 导入文件出错
			throw new RuntimeException("数据导入出现错误" + result.getErrors());
		}
		List<Weakness> weaknessList = result.getListBean();
		if (null == weaknessList || weaknessList.size() == 0)
			return;
		for (Weakness weak : weaknessList) {
			System.out.println(weak);
			weaknessDao.save(weak);
		}
	}

}
